# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=wasm-pack
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.10.3
pkgrel=1
pkgdesc="Your favorite rust -> wasm workflow tool! (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/rustwasm/wasm-pack"
license=('MIT' 'Apache')
depends=(
  "${MINGW_PACKAGE_PREFIX}-bzip2"
  "${MINGW_PACKAGE_PREFIX}-curl"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-mdbook"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)
options=('staticlibs' 'strip')
source=("$_realname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz")
sha256sums=('a4596c08dca32e2f0a1bfe1215421981943b66977846b573c13ea4a7e71fc94c')

prepare() {
  cd "${srcdir}"
  rm -rf "build-${MSYSTEM}" | true
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/cargo fetch \
    --locked
}

build() {
  msg "Cargo build for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/cargo build \
    --release \
    --locked \
    --no-default-features --features sys-openssl

  msg "Build documentation"
  cd "${srcdir}/build-${MSYSTEM}/docs"
  ${MINGW_PREFIX}/bin/mdbook build
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/cargo test \
    --release \
    --locked
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 "target/release/wasm-pack.exe" "${pkgdir}${MINGW_PREFIX}/bin/wasm-pack.exe"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/"
  cp -r -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html/" docs/book/*

  install -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  cp -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/" LICENSE-{APACHE,MIT}
}

